<template>
  <!-- 搜索区域 -->
  <ContentWrap>
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="100px"
    >
      <el-form-item label="用户类型" prop="userType">
        <el-select
          v-model="queryParams.userType"
          @change="handleUserTypeChange"
          placeholder="请选择用户类型"
          class="!w-240px"
        >
          <el-option
            v-for="dict in common_user_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
        <!--        <el-radio-group-->
        <!--          v-model="queryParams.userType"-->
        <!--          @change="handleUserTypeChange"-->
        <!--          class="!w-240px"-->
        <!--        >-->
        <!--          <el-radio v-for="dict in common_user_type" :key="dict.value" :value="dict.value">-->
        <!--            {{ dict.label }}-->
        <!--          </el-radio>-->
        <!--        </el-radio-group>-->
      </el-form-item>
      <el-form-item label="用户编号" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="访问令牌" prop="accessToken">
        <el-input
          v-model="queryParams.accessToken"
          placeholder="请输入访问令牌"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="客户端编号" prop="clientId">
        <el-input
          v-model="queryParams.clientId"
          placeholder="请输入客户端编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" />
          重置
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表区域 -->
  <ContentWrap>
    <el-table v-loading="listLoading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="用户信息" prop="userId" width="260">
        <template #default="scope">
          <span v-if="scope.row.userType == UserTypeEnum.Staff">
            <el-tooltip :content="scope.row.userId" placement="right">
            {{ scope.row.userName }}
            </el-tooltip>
          </span>
          <span v-else>{{ scope.row.userId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户类型" prop="userType" width="130">
        <template #default="scope">
          <dict-tag :options="common_user_type" :value="scope.row.userType" />
        </template>
      </el-table-column>
      <el-table-column label="访问令牌" prop="accessToken" />
      <el-table-column label="刷新令牌" prop="refreshToken" />
      <el-table-column label="客户端编号" prop="clientId" width="200" />
      <el-table-column label="过期时间" prop="expiresTime" width="180px" />
      <el-table-column label="创建时间" prop="createTime" width="180px" />
      <el-table-column label="操作" width="100px" fixed="right">
        <template #default="scope">
          <el-button
            link
            type="danger"
            @click="handleForceLogout(scope.row.accessToken)"
            v-hasPermi="['system:oauth2-token:delete']"
          >
            强退
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页区域 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
</template>

<script setup lang="ts">
import { OAuth2TokenApi, OAuth2AccessTokenVO } from '@/api/system/oauth2/oauth2_token'
import { UserTypeEnum } from '@/utils/constants'

/** OAuth2访问令牌 列表 */
defineOptions({ name: 'SystemOAuth2AccessToken' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const { common_user_type } = useDict(DICT_TYPE.common_user_type)

const listLoading = ref(true) // 列表的加载中状态
const list = ref<OAuth2AccessTokenVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  userId: undefined,
  userType: UserTypeEnum.Staff,
  accessToken: undefined,
  clientId: undefined,
  createTime: [],
  pageNo: 1,
  pageSize: 10
})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  listLoading.value = true
  try {
    const data = await OAuth2TokenApi.getAccessTokenPage(queryParams)
    list.value = data.content
    total.value = data.totalElement
  } finally {
    listLoading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

const handleUserTypeChange = () => {
  handleQuery()
}

const handleForceLogout = async (accessToken: string) => {
  try {
    // 删除的二次确认
    await message.confirm('是否要强制退出用户')
    // 发起删除
    await OAuth2TokenApi.deleteAccessToken(accessToken)
    message.success(t('common.success'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>

<style scoped lang="scss"></style>
